package action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.bean.Chat;
import model.bo.DiscussBO;
import form.DiscussForm;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/** 
 * Action Class ExampleXMLAction
 */
public class DiscussAction extends Action {

	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		DiscussForm discussForm = (DiscussForm) form;
		DiscussBO discussBo = new DiscussBO();
		ArrayList<Chat> listChat = new ArrayList<Chat>();
		String submit = request.getParameter("Submit");
		String userName = (String) session.getAttribute("userName");
		String comment = request.getParameter("content");
		String delete = request.getParameter("btnDelete");
        String url = request.getServletPath();
        if(request.getQueryString()!=null){
        	url+="?"+request.getQueryString();
        }
		session.setAttribute("url", url);
		if ("Gửi thảo luận".equals(submit)) {
			System.out.println("da nhan nut gui thao luan");
			if (userName == null || userName == "") {
				request.setAttribute("NOK", "NOK");
			} else if ("Hãy chia sẽ ý kiến của bạn...".equals(comment)) {
				System.out.println("ok");
				request.setAttribute("NOK1", "NOK1");
			} else {
				discussBo.InsertChat(userName, comment);
			}
		}
		if("Delete".equals(delete)){
			System.out.println("da nhan nut delete");
			int chatId = discussForm.getChatId();
			System.out.println(chatId + "chat ID cua comment duoc chon");
			if(chatId != 0){
				discussBo.DeleteChat(chatId);
			}
		}
		listChat = discussBo.getChat();
		discussForm.setListChat(listChat);
		request.setAttribute("listChat", listChat);
		return mapping.findForward("OK");
	}


}